package com.it.travel01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.travel01.dao.FavoriteDao;
import com.it.travel01.pojo.Favorite;
import com.it.travel01.pojo.User;
import com.it.travel01.service.FavoriteService;
import com.it.travel01.vo.PageBean;
import com.it.travel01.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service("favoriteService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao,Favorite> implements FavoriteService {

    @Value("${constant.userkey}")
    private String userkey;

    /**
     * 收藏回显功能
     * @param rid 查询详情路线id
     * @return
     */
    @Override
    public ResultInfo isFavoriteByRid(Integer rid) {
//        前端发送旅游路线ID到后台，后台从session下获取用户信息进行判断：
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes attributes=(ServletRequestAttributes)requestAttributes;
        User user = (User) attributes.getRequest().getSession().getAttribute(userkey);
        if (user == null) {
//        1.如果session下的用户信息未null，则返回false且携带用户未登录的状态信息；
            return new ResultInfo(false, "用户未登录，请先登录");
        }
//        2.如果用户存在，根据用户id和旅游路线id查询：
//        Favorite favorite = favoriteDao.isFavoriteByRid(user.getUid(), rid);
        LambdaQueryWrapper<Favorite> queryWrapper = Wrappers.lambdaQuery(Favorite.class);
        queryWrapper.eq(Favorite::getRid, rid)
                .eq(Favorite::getUid, user.getUid());
        Favorite favorite = this.getOne(queryWrapper);
        if (favorite == null) {
//        2.2 收藏不存在，则返回flag:true, data:false
            return new ResultInfo(true,false,null);
        }
//        2.1 收藏存在，则返回flag:true data:true
        return new ResultInfo(true, true,null);

    }


    /**
     * 我的收藏展示
     * @param curPage
     * @return
     */
    @Override
    public ResultInfo findFavoriteByPage(Integer curPage) {
//        PageHelper.startPage(curPage,12);
        Page<Favorite> favoritePage = new Page<>(curPage, 12);

        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes attributes=(ServletRequestAttributes)requestAttributes;

        User user = (User) attributes.getRequest().getSession().getAttribute(userkey);

        if (user == null) {
            new ResultInfo(false, "用户未登录");
        }

        List<Favorite> favoriteList = baseMapper.findFavoriteByPage(user.getUid());

//        PageInfo<Favorite> favoritePageInfo = new PageInfo<>(favoriteList);
        PageBean<Favorite> routePageBean = new PageBean<>();
        routePageBean.setTotalPage(favoritePage.getPages());
        routePageBean.setPrePage(favoritePage.getCurrent() == 1L ? 1L : favoritePage.getCurrent() - 1);
        routePageBean.setNextPage(favoritePage.getCurrent() == favoritePage.getPages() ? favoritePage.getPages() : favoritePage.getCurrent() + 1);
        routePageBean.setFirstPage(1L);
        routePageBean.setCurPage(favoritePage.getCurrent());
        routePageBean.setData(favoriteList);
        routePageBean.setCount(favoritePage.getTotal());

        return new ResultInfo(true,routePageBean,null);
    }
}
